package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.Comment;
import com.sywb.chuangyebao.contract.NewsDetailContract;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.dialog.CommentReplyDialog;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.utils.SoftKeyboardUtils;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.widget.PullToRefreshView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 资讯详情
 */
public class NewsDetailActivity extends SupportCommentDetailActivity<NewsDetailContract.Presenter> implements NewsDetailContract.View {

    @BindView(R.id.rv_comment)
    RecyclerView rvComment;
    @BindView(R.id.common_nodata_content)
    TextView commonNodataContent;
    @BindView(R.id.common_nodata_icon)
    ImageView commonNodataIcon;
    @BindView(R.id.common_nodata_subtitle)
    TextView commonNodataSubtitle;
    @BindView(R.id.common_nodata)
    RelativeLayout commonNodata;
    @BindView(R.id.common_recycler_layout)
    RelativeLayout commonRecyclerLayout;

    @BindView(R.id.ll_showinfo)
    LinearLayout llShowInfo;
    @BindView(R.id.tv_content)
    TextView tvContent;

    @BindView(R.id.iv_comment)
    ImageView ivComment;
    @BindView(R.id.tv_comment_count)
    TextView tvCommentCount;
    @BindView(R.id.iv_collect)
    ImageView ivCollect;
    @BindView(R.id.iv_share)
    ImageView ivShare;

    @BindView(R.id.ll_write_comment)
    LinearLayout llWriteComment;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_send)
    TextView tvSend;

    @BindView(R.id.tv_consult)
    TextView tvConsult;

    private int articleId;
    private Comment mComment; // 如果有此参数，表示从个人中心我的评论进入，当加载完文章后需要自动弹出CommentReplyDialog

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_news_detail;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        if (bundle == null) {
            bundle = getIntent().getExtras() == null ? new Bundle() : getIntent().getExtras();
        }

        articleId = bundle.getInt("p0", 0);
        mComment = (Comment) bundle.getSerializable("p1");

        setTitle("资讯详情");

        addMenuImageItme(R.drawable.news_more_normal, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.showDialog();
            }
        });
    }

    @OnClick({R.id.tv_content, R.id.iv_comment, R.id.tv_comment_count, R.id.iv_collect, R.id.iv_share, R.id.tv_send, R.id.tv_consult})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_content:

                if (!Utils.checkIsNext(getMyFragmentManager())) {
                    return;
                }
                
                llWriteComment.setVisibility(View.VISIBLE);
                llShowInfo.setVisibility(View.GONE);

                etContent.setFocusable(true);
                etContent.setFocusableInTouchMode(true);
                etContent.requestFocus();
                etContent.requestFocusFromTouch();

                SoftKeyboardUtils.openSoftKeybord(etContent);
                break;
            case R.id.iv_comment:
            case R.id.tv_comment_count:
                mPresenter.goViewTop();
                break;
            case R.id.iv_collect:
                mPresenter.favorite();
                break;
            case R.id.iv_share:
                mPresenter.showDialog();
                break;
            case R.id.tv_send:
                String content = etContent.getText().toString();
                if (content.isEmpty()) {
                    ToastUtils.show(mContext, "请输入评论内容");
                } else {
                    mPresenter.addComment(content);
                    etContent.setText("");
                    SoftKeyboardUtils.stopSoftKeybord(etContent);
                    llShowInfo.setVisibility(View.VISIBLE);
                    llWriteComment.setVisibility(View.GONE);
                }
                break;
            case R.id.tv_consult:
                if (mPresenter != null) {
                    mPresenter.goConsult();
                }
                break;
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.HTTP_COMMENTREPLY_ADD)})
    public void commentCount(String data) {
        Logger.e("commentCount:" + data);
        String[] datas = data.split("&");
        mPresenter.changeCommentCount(Integer.valueOf(datas[1]), Integer.valueOf(datas[2]));
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_THUMB)})
    public void commentThumb(String data) {
        Logger.e("commentThumb:" + data);
        String[] datas = data.split("&");
        mPresenter.changeCommentThumb(Integer.valueOf(datas[1]), Boolean.valueOf(datas[2]), Integer.valueOf(datas[3]));
    }

    public int getPageNum() {
        return 3;
    }

    public int getProjectId() {
        return 0;
    }

    public int getNewsId() {
        return articleId;
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return rvComment;
    }

    @Override
    public void setNoData(boolean isNoData) {
    }

    @Override
    public int getDialogViewHeight() {
        return actionbar.getMeasuredHeight() + content.getMeasuredHeight();
    }

    @Override
    public void changeCommentCount(int count) {
        if (count > 0) {
            tvCommentCount.setText(RegexUtils.getFormatNum(count));
            tvCommentCount.setVisibility(View.VISIBLE);
        } else {
            tvCommentCount.setVisibility(View.GONE);
        }
    }

    @Override
    public void setIsFavorite(boolean isFavorite) {
        if (isFavorite) {
            ivCollect.setImageResource(R.drawable.collect_bb_fill);
        } else {
            ivCollect.setImageResource(R.drawable.collect_bb);
        }
    }

    @Override
    public int getArticleId() {
        return articleId;
    }

    @Override
    public void setConsultState(boolean isConsult) {
        tvConsult.setVisibility(isConsult ? View.VISIBLE : View.GONE);
    }

    @Override
    public PullToRefreshView getPullToRefreshView() {
        return null;
    }


    @Override
    public Comment getComment() {
        return mComment;
    }
}
